module.exports = app => {
    const Router = require("koa-router")
    const router = new Router()
    const bodyParser = require("koa-bodyparser")
    app.use(bodyParser())
    app.use(router.routes()).use(router.allowedMethods())
    const Moive = require("./datebase.js")
    router.get('/getData', async ctx => {
        const {pageNum = 1, title = "", country = "", category = "", year = ""} = ctx.request.query;
        const titleReg = new RegExp(title);
        const countryReg = new RegExp(country);
        const categoryReg = new RegExp(category);
        let yearReg;
        if( + year >=2010) {
            yearReg = new RegExp(year);
        }
        else if( 2000 <= + year && + year <= 2009){
            yearReg = new RegExp(/200/);
        }
        else if( 1000 <= + year && + year <= 1999){
            yearReg = new RegExp(/19/);
        }
        else {
            yearReg = new RegExp("");
        }
        const total = (await Moive.find({
            title: titleReg,
            country: countryReg,
            category: categoryReg,
            year: yearReg,
        })).length;
        const data = await Moive.find({
            title: titleReg,
            country: countryReg,
            category: categoryReg,
            year: yearReg,
        }).skip((pageNum - 1) * 10).limit(10);
        ctx.body = {
            success: true,
            message: "获取成功",
            total,
            data
        }
    });
    router.get("/getDetail", async ctx => {
        const {_id} = ctx.request.query;
        const data = await Moive.findOne({_id});
        ctx.body = {
            success: true,
            message: "获取成功",
            data
        }
    })
}